import 'package:async/async.dart';
import 'package:boss/models/material.dart' as material;
import 'package:boss/provider/material_provider.dart';
import 'package:boss/ui/page/commodity/material/page/materialDetail/material_detail_page.dart';
import 'package:boss/ui/widget/loader/empty_widget.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'material_item.dart';

class MaterialList extends StatelessWidget {
  Future<Result<List<material.Material>>> _loadTask(BuildContext context) async {
    var materialProvider = Provider.of<MaterialProvider>(
      context,
      listen: false,
    );
    return materialProvider.initData();
  }

  @override
  Widget build(BuildContext context) {
    return Loader<List<material.Material>>(
      loadTask: () => _loadTask(context),
      builder: (context) {
        return Consumer<MaterialProvider>(
          builder: (context, materialProvider, child) {
            if (materialProvider.materials.length == 0) return EmptyWidget();

            return ListView.builder(
              padding: EdgeInsets.symmetric(
                horizontal: 30.w,
              ),
              itemBuilder: (context, index) {
                var current = materialProvider.materials[index];

                return MaterialItem(
                  data: current,
                  onTap: () {
                    Navigator.pushNamed(context, MaterialDetailPage.pageName, arguments: {
                      'data': current,
                    });
                  },
                );
              },
              itemCount: materialProvider.materials.length,
            );
          },
        );
      },
    );
  }
}
